package com.laxcen.smoke.service.impl;

import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.LogContent;
import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.common.QueriedLog;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.GetLogsRequest;
import com.aliyun.openservices.log.response.GetLogsResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laxcen.smoke.dao.LogDao;
import com.laxcen.smoke.dto.res.LogWebRes;
import com.laxcen.smoke.service.LogService;
import com.laxcen.smoke.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogDao logDao;

    @Value("${accessKey}")
    private String accessId;

    @Value("${accessSecret}")
    private String accessKey;

    @Override
    public Response<PageInfo<LogWebRes>> getWebLogList(int pageNo, int pageSize) {
        Page page = PageHelper.startPage(pageNo,pageSize);
        List<LogWebRes> list = logDao.getWebLogList();
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(list);
        Response<PageInfo<LogWebRes>> response = new Response<>();
        response.setData(pageInfo);
        response.setOK();
        return response;
    }

    @Override
    public Response<List<Map>> getAppLogList(String starttime,String endtime,String devicename) throws LogException, ParseException {

//        String accessId = "LTAI5tQqhoe1XSrkoCWkDDQ9";    //阿里云访问密钥AccessKey ID。更多信息，请参见访问密钥。阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维。
//        String accessKey = "MXycRe3SmNlDPKuLNt1cfxhFyjk5Cf";  //阿里云访问密钥AccessKey Secret。
        String host = "cn-shanghai.log.aliyuncs.com";  //日志服务的域名。更多信息，请参见服务入口。此处以杭州为例，其它地域请根据实际情况填写。
        Client client = new Client(host, accessId, accessKey);   //创建日志服务Client。
        //创建Project和Logstore。
        String project = "iot-log-1070277949580075-cn-shanghai";  //待创建的Project的名称。
        String logstore = "iot-logs";     //待创建的Logstore的名称。
        Date date = new Date();
        int to = 0;
        int from = 0;
        if ("".equals(starttime)&&"".equals(endtime)) {
             to = (int) (date.getTime() * 1.0 / 1000);
             from = to - 86400; //减去一天的时间戳
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date datestart = sdf.parse(starttime);
            Date dateend = sdf.parse(endtime);
            to = (int) (dateend.getTime() * 1.0 / 1000);
            from = (int) (datestart.getTime() * 1.0 / 1000);
        }
        GetLogsRequest request = new GetLogsRequest(project,logstore,from,to,"","uplink or ThingModel or device");
            GetLogsResponse response = client.GetLogs(request);
            ArrayList<QueriedLog> queriedLogs = response.GetLogs();
            List<Map>list = new LinkedList<>();
            for (QueriedLog q : queriedLogs){
                LogItem logItem = q.GetLogItem();
                ArrayList<LogContent> logContents = logItem.GetLogContents();
                Map<String,String> map = new HashMap<String,String>();

                for (LogContent log : logContents){
                    map.put(log.mKey,log.mValue);

                    if ("".equals(devicename)){
                        if ("device".equals(log.mValue)||"uplink".equals(log.mValue)||"ThingModel".equals(log.mValue)) {
                            list.add(0, map);
                        }
                    }else {
                        if ("deviceName".equals(log.mKey)&&devicename.equals(log.mValue)) {

                            list.add(0, map);
                        }
                    }


                }


            }
            Response<List<Map>> response1 = new Response<>();
            response1.setOK();
            response1.setData(list);
            return response1;
    }
}
